﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Motorway_Simulator.GUI;
using Motorway_Simulator.FileIO;

namespace Motorway_Simulator.GUI.Designer
{
    public partial class DesignPanel : Form
    {
        // Holds a list view of road units
        private ListView roadUnitListView;
        public delegate void RoadUnitOrientationDelegate(int orientation);
        public RoadUnitOrientationDelegate RoadUnitOrientationCallback;
        public delegate void StatusLabelDelegate(string message);
        public StatusLabelDelegate StatusLabelCallBack;
        public bool isDelete;

        // North South East West of road unit as INT
        private int roadUnitOrientation;
        private string selectedRoadUnit;

        public DesignPanel(ListView roadUnitListView)
        {
            this.roadUnitListView = roadUnitListView;
            InitializeComponent();

            designCanvas1.StatusLabelCallBack = new DesignCanvas.StatusLabelDelegate(statusLabelCallBack);
            
            // Default orientation facing North
            roadUnitOrientation = 0;
            selectedRoadUnit = "Straight";
            isDelete = false;

            roadUnitListView.MouseClick += new MouseEventHandler(roadUnitListView_MouseClick);
            roadUnitListView.KeyDown += new KeyEventHandler(roadUnitListView_KeyDown);
        }

        /// <summary>
        /// Handles SPACE key event for rotating image direction
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void roadUnitListView_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Space)
            {
                if (roadUnitListView.SelectedItems.Count != 0)
                {
                    if (roadUnitOrientation < 3)
                    {
                        roadUnitOrientation++;
                    }
                    else
                    {
                        // Reset to 0, facing North
                        roadUnitOrientation = 0;
                    }
                    // Notify canvas of change
                    designCanvas1.RoadUnitOrientation = roadUnitOrientation;
                    RoadUnitOrientationCallback(roadUnitOrientation);
                }
            }
        }

        /// <summary>
        /// Returns the road unit orientation
        /// </summary>
        public int RoadUnitOrientation
        {
            get { return roadUnitOrientation; }
        }

        /// <summary>
        /// Handles event for roadUnitlistview and get the selected text
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event</param>
        private void roadUnitListView_MouseClick(object sender, MouseEventArgs e)
        {
            if (roadUnitListView.SelectedItems.Count != 0)
            {
                selectedRoadUnit = roadUnitListView.SelectedItems[0].Text;
            }
        }

        // On click event which is called when user click on the grid
        private void PlaceUnit(object sender, MouseEventArgs e)
        {
            // Check if it is selected AND if delete is not selected
            if (roadUnitListView.SelectedItems.Count != 0 && !isDelete)
            {   // Points
                int x = e.X;
                int y = e.Y;

                // Pass the raw position to design canvas
                designCanvas1.AddRoadUnit(selectedRoadUnit, x, y);
                }

            if (isDelete)
            {
                int x = e.X;
                int y = e.Y;
                designCanvas1.DeleteRoadUnit(x, y);
            }

        }

        /// <summary>
        /// Method callback to set message on the GUI
        /// </summary>
        /// <param name="message"></param>
        private void statusLabelCallBack(string message)
        {
            StatusLabelCallBack(message);
        }

        /// <summary>
        /// Is being set by the Main GUI when deletebutton is checked
        /// or not
        /// </summary>
        public bool IsDelete
        {
            set { isDelete = value; }
        }

        /// <summary>
        /// Method which saves design
        /// </summary>
        /// <param name="fileName">File Name</param>
        /// <returns>If saved successfully</returns>
        public bool SaveDesign(string fileName)
        {
            bool isSaved = false;
            FileAccessor fileAccessor = new FileAccessor();
            isSaved = fileAccessor.Save(fileName, designCanvas1.GetDesignRoadUnits());
            return isSaved;
        }

        /// <summary>
        /// Loads design into design canvas
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>If loads successfully</returns>
        public bool LoadDesign(string fileName)
        {
            FileAccessor fileAccessor = new FileAccessor();
            object loadedObject = fileAccessor.Load(fileName);
            return designCanvas1.LoadDesign(loadedObject);
        }

        /// <summary>
        /// Saves the design simulation format
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>Successful if it's saved correctly</returns>
        internal bool SaveDesignSimulation(string fileName)
        {
            bool isSaved = false;
            FileAccessor fileAccessor = new FileAccessor();
            SimObject simObject = designCanvas1.AddLanesToSimObject(new SimObject());
            simObject.RoadUnits = designCanvas1.RoadUnitMap;
            
            isSaved = fileAccessor.Save(fileName, simObject);
            return isSaved;
        }
    }
}
